package com.kx.smsolatform.mq;

import com.kx.smsolatform.service.SearcherService;
import com.kx.smsplatform.common.constants.RabbitMQConstants;
import com.kx.smsplatform.common.model.StandardSubmit;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 监听消息队列，发送消息到es中进行写日志
 *
 * @Author Sun
 * @Date 2021/7/6 17:38
 */
@Slf4j
@Component
public class SendLogListener {
    @Autowired
    private SearcherService searcherService;

    @RabbitListener(queues = {RabbitMQConstants.TOPIC_SMS_SEND_LOG})
    public void consume(StandardSubmit submit, Channel channel, Message message) throws IOException {
        log.info("【搜索模块 写日志操作】已接收队列：{}的消息，内容为submit={}", RabbitMQConstants.TOPIC_SMS_SEND_LOG, submit);

        //写日志到es中
        searcherService.addLog(submit);
        //手动ACK
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
